﻿package org.flixel
{
    import flash.events.*;
    import flash.net.*;

    public class FlxSave extends Object
    {
        public var data:Object;
        public var name:String;
        protected var _sharedObject:SharedObject;
        protected var _onComplete:Function;
        protected var _closeRequested:Boolean;
        static var SUCCESS:uint = 0;
        static var PENDING:uint = 1;
        static var ERROR:uint = 2;

        public function FlxSave()
        {
            this.destroy();
            return;
        }// end function

        public function destroy() : void
        {
            this._sharedObject = null;
            this.name = null;
            this.data = null;
            this._onComplete = null;
            this._closeRequested = false;
            return;
        }// end function

        public function bind(param1:String) : Boolean
        {
            var Name:* = param1;
            this.destroy();
            this.name = Name;
            try
            {
                this._sharedObject = SharedObject.getLocal(this.name);
            }
            catch (e:Error)
            {
                FlxG.log("ERROR: There was a problem binding to\nthe shared object data from FlxSave.");
                destroy();
                return false;
            }
            this.data = this._sharedObject.data;
            return true;
        }// end function

        public function close(param1:uint = 0, param2:Function = null) : Boolean
        {
            this._closeRequested = true;
            return this.flush(param1, param2);
        }// end function

        public function flush(param1:uint = 0, param2:Function = null) : Boolean
        {
            var MinFileSize:* = param1;
            var OnComplete:* = param2;
            if (!this.checkBinding())
            {
                return false;
            }
            this._onComplete = OnComplete;
            var result:String;
            try
            {
                result = this._sharedObject.flush(MinFileSize);
            }
            catch (e:Error)
            {
                return onDone(ERROR);
            }
            if (result == SharedObjectFlushStatus.PENDING)
            {
                this._sharedObject.addEventListener(NetStatusEvent.NET_STATUS, this.onFlushStatus);
            }
            return this.onDone(result == SharedObjectFlushStatus.FLUSHED ? (SUCCESS) : (PENDING));
        }// end function

        public function erase() : Boolean
        {
            if (!this.checkBinding())
            {
                return false;
            }
            this._sharedObject.clear();
            return true;
        }// end function

        protected function onFlushStatus(event:NetStatusEvent) : void
        {
            this._sharedObject.removeEventListener(NetStatusEvent.NET_STATUS, this.onFlushStatus);
            this.onDone(event.info.code == "SharedObject.Flush.Success" ? (SUCCESS) : (ERROR));
            return;
        }// end function

        protected function onDone(param1:uint) : Boolean
        {
            switch(param1)
            {
                case PENDING:
                {
                    FlxG.log("FLIXEL: FlxSave is requesting extra storage space.");
                    break;
                }
                case ERROR:
                {
                    FlxG.log("ERROR: There was a problem flushing\nthe shared object data from FlxSave.");
                    break;
                }
                default:
                {
                    break;
                    break;
                }
            }
            if (this._onComplete != null)
            {
                this._onComplete(param1 == SUCCESS);
            }
            if (this._closeRequested)
            {
                this.destroy();
            }
            return param1 == SUCCESS;
        }// end function

        protected function checkBinding() : Boolean
        {
            if (this._sharedObject == null)
            {
                FlxG.log("FLIXEL: You must call FlxSave.bind()\nbefore you can read or write data.");
                return false;
            }
            return true;
        }// end function

    }
}
